/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.assertj.core.util.Lists;
import org.json.JSONObject;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.*;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.enums.WorkOrderTypeEnum;
import org.springblade.modules.admin.service.*;
import org.springblade.modules.admin.mapper.UserSalaryMapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.utils.DateUtils;
import org.springblade.modules.admin.vo.ElevatorEffectedVO;
import org.springblade.modules.admin.vo.MaintainPlanContentVO;
import org.springblade.modules.admin.vo.sugar.MalfunctionScreenVO;
import org.springblade.modules.admin.vo.user.SalaryVo;
import org.springblade.modules.admin.vo.user.UserWithoutVo;
import org.springblade.modules.admin.vo.user.WxUserGroupPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户工资表 服务实现类
 *
 * @author ysq
 * @since 2025-05-22
 */
@Service
public class UserSalaryServiceImpl extends BaseServiceImpl<UserSalaryMapper, UserSalary> implements IUserSalaryService {


	@Autowired
	private ElevatorPrincipalDeptService elevatorPrincipalDeptService;

	@Autowired
	private IUserSalaryRulesService userSalaryRulesService;

	@Autowired
	private IUserSalaryMonthDetailService userSalaryMonthDetailService;

	@Autowired
	private IUserSalaryMonthService userSalaryMonthService;

	@Autowired
	private IUserSalaryRulesHistoryService userSalaryRulesHistoryService;

	@Override
	public List<UserSalary> list(UserSalary userSalary) {
		return baseMapper.list(userSalary);
	}


	@Override
	@Transactional
	public List<UserSalaryMonthDetail> getAll(UserSalaryRules userSalaryRules) {
		UserSalary search = new UserSalary();
		search.setUserIds(userSalaryRules.getUserIds());
		List<UserSalary> list = baseMapper.list(search);
		List<ElevatorPrincipalDept> elevatorPrincipalDepts = elevatorPrincipalDeptService.listAll();
		//工资规则
		List<UserSalaryRules> rulesList = userSalaryRulesService.list(Wrappers.<UserSalaryRules>lambdaQuery().eq(UserSalaryRules::getTemplateId, userSalaryRules.getTemplateId()));
		if (rulesList.size() == 0) {
			throw new ServiceException("未找到对应的模板,请刷新页面重新选择");
		}
		//数据初始化好-每个人默认给第一个组
		for (UserSalary userSalary : list) {
			if (StrUtil.isNotEmpty(userSalary.getGroupIds())) {
				userSalary.setGroupId(Convert.toLong(Lists.list(userSalary.getGroupIds().split(",")).get(0)));
			}
			userSalary.setSkillName(Convert.toStr(userSalary.getSkillName(), ""));
		}


		//故障率的计算
		Map<Long, List<WxUserGroupPlanVo>> userPlanMap2 = getFaultRateMap(userSalaryRules, list);

		//担当比例的查询
		List<WxUserGroupPlanVo> wxUserGroupPlanVos = baseMapper.getWxUserGroupPlanVoV2(userSalaryRules);
		Map<Long, List<WxUserGroupPlanVo>> userPlanMap = getMyWxPlanRateUserId(list, wxUserGroupPlanVos);
		Map<Long, List<WxUserGroupPlanVo>> userPlanMap3 = getMyWxPlanRateGroupId(list, wxUserGroupPlanVos);

		//电梯用途 0-公用(年审)，1-私用-备注成别墅梯
		Map<Long, List<ElevatorPrincipalDept>> listMap0 = elevatorPrincipalDepts.stream().filter(o -> o.getUseType() == 0).collect(Collectors.groupingBy(ElevatorPrincipalDept::getDeptId));
		Map<Long, List<ElevatorPrincipalDept>> listMap1 = elevatorPrincipalDepts.stream().filter(o -> o.getUseType() == 1).collect(Collectors.groupingBy(ElevatorPrincipalDept::getDeptId));

		Map<Long, List<UserSalary>> listMap2 = list.stream().collect(Collectors.groupingBy(UserSalary::getGroupId));


		//保养不合格单数
		List<WxUserGroupPlanVo> byPlanList = baseMapper.getNullifyPlanCountsBy(userSalaryRules);
		Map<Long, List<WxUserGroupPlanVo>> byPlanListMap = byPlanList.stream().collect(Collectors.groupingBy(WxUserGroupPlanVo::getGroupId));

		//维修不合格单数
		List<WxUserGroupPlanVo> wxPlanList = baseMapper.getNullifyPlanCountsWx(userSalaryRules);
		Map<Long, List<WxUserGroupPlanVo>> wxPlanListMap = wxPlanList.stream().collect(Collectors.groupingBy(WxUserGroupPlanVo::getUserId));

		//开始计算每个人的工资.........................................................
		List<UserSalaryMonthDetail> userSalaryMonthDetails = new ArrayList<>(100);

		BigDecimal technicalSupportFaultRate = userSalaryRules.getTechnicalSupportFaultRate();
		for (UserSalary userSalary : list) {
			UserSalaryMonthDetail userSalaryMonthDetail = new UserSalaryMonthDetail();
			userSalaryMonthDetail.setTemplateId(userSalaryRules.getTemplateId());
			userSalaryMonthDetail.setTemplateName(userSalaryRules.getTemplateName());

			//1:封装姓名
			userSalaryMonthDetail.setUserId(userSalary.getUserId());
			userSalaryMonthDetail.setUserName(userSalary.getUserName());

			//2:封装岗位
			userSalaryMonthDetail.setRoleNames(userSalary.getRoleNames());

			//3:封装所在组
			userSalaryMonthDetail.setGroupNames(userSalary.getGroupNames());

			//4:封装技能等级
			userSalaryMonthDetail.setSkillName(userSalary.getSkillName());

			//5:封装是否技术支持
			userSalaryMonthDetail.setIsExpert(userSalary.getRoleAlias().contains("technical_support"));

			List<UserWithoutVo> userWithoutVos = userSalaryRules.getUserWithoutVos();

			//技术支持计算
			if (userSalaryMonthDetail.getIsExpert()) {
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);

						//11:申请技术支持次数
						userSalaryMonthDetail.setExpertHelpCounts(Convert.toInt(userWithoutVo.getJsPlanCounts(), 0));

						//12:技术支持判定-维修责任次数
						userSalaryMonthDetail.setWxExpertHelpCounts(Convert.toInt(userWithoutVo.getWxPlanCounts(), 0));

						//13:技术支持判定-保养责任次数
						userSalaryMonthDetail.setByExpertHelpCounts(Convert.toInt(userWithoutVo.getByPlanCounts(), 0));

						//14:技术支持他人次数
						userSalaryMonthDetail.setExpertHelpOtherCounts(Convert.toInt(userWithoutVo.getJsAwardPlanCounts(), 0));
					}
				}

				//基础工资
				userSalaryMonthDetail.setUserSalary(userSalary.getSalary());
				//16:岗位薪资
				UserSalaryRules salaryRules = rulesList.stream().filter(o -> o.getRulesType() == 16).collect(Collectors.toList()).get(0);
				List<SalaryVo> rulesDetail = salaryRules.getRulesDetail();
				userSalaryMonthDetail.setRoleSalary(Convert.toBigDecimal(0));
				for (SalaryVo salaryVo : rulesDetail) {
					if (userSalary.getSkillName().equals(salaryVo.getKey())) {
						userSalaryMonthDetail.setRoleSalary(Convert.toBigDecimal(salaryVo.getValue()));
						break;
					}
				}
				//17:计算保养绩效
				getByPerformance(userSalaryMonthDetail, userSalary, rulesList);
				userSalaryMonthDetail.setByPerformance(userSalaryMonthDetail.getByPerformance().setScale(2, BigDecimal.ROUND_HALF_UP));

				userSalaryMonthDetail.setFaultRate(technicalSupportFaultRate);
				//20:故障率绩效处罚
				UserSalaryRules salaryRules5 = rulesList.stream().filter(o -> o.getRulesType() == 20).collect(Collectors.toList()).get(0);
				SalaryVo salaryVo1 = salaryRules5.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0);
				SalaryVo salaryVo2 = salaryRules5.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0);
				if ((technicalSupportFaultRate.multiply(Convert.toBigDecimal(100))).compareTo(Convert.toBigDecimal(salaryVo1.getValue())) == 1) {
					BigDecimal bigDecimal = (userSalaryMonthDetail.getByPerformance().add(userSalaryMonthDetail.getWxPerformance())).multiply((userSalaryMonthDetail.getFaultRate().subtract(Convert.toBigDecimal(salaryVo1.getValue()).divide(Convert.toBigDecimal(100)))).multiply((Convert.toBigDecimal(salaryVo2.getValue()))));
					userSalaryMonthDetail.setFailureRatePunish(bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));
				} else {
					userSalaryMonthDetail.setFailureRatePunish(Convert.toBigDecimal(0));
				}

				//23判断保养责任处罚---保养责任工单数--手动输入
				UserSalaryRules salaryRules23 = rulesList.stream().filter(o -> o.getRulesType() == 23).collect(Collectors.toList()).get(0);
				String value23_1 = salaryRules23.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value23_2 = salaryRules23.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				int userCounts = listMap2.get(userSalary.getGroupId()).size();
				userSalaryMonthDetail.setByLiabilityPunish(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getByPlanCounts() != null && userWithoutVo.getByPlanCounts() > 0) {
							if (userWithoutVo.getByPlanCounts() > userCounts) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getByPlanCounts()).subtract(Convert.toBigDecimal(userCounts).multiply(Convert.toBigDecimal(value23_2)))).multiply(Convert.toBigDecimal(value23_1)).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setByLiabilityPunish(bigDecimal);
								}

							}
						}
					}
				}

				//24判断维修责任处罚---维修责任工单数--手动输入
				UserSalaryRules salaryRules24 = rulesList.stream().filter(o -> o.getRulesType() == 24).collect(Collectors.toList()).get(0);
				String value24_1 = salaryRules24.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value24_2 = salaryRules24.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				userSalaryMonthDetail.setWxLiabilityPunish(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getWxPlanCounts() != null && userWithoutVo.getWxPlanCounts() > 0) {
							if (userWithoutVo.getWxPlanCounts() > userCounts) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getWxPlanCounts()).subtract(Convert.toBigDecimal(userCounts).multiply(Convert.toBigDecimal(value24_2)))).multiply(Convert.toBigDecimal(value24_1)).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setWxLiabilityPunish(bigDecimal);
								}
							}
						}
					}
				}

				//25申请技术处罚--手动输入
				UserSalaryRules salaryRules25 = rulesList.stream().filter(o -> o.getRulesType() == 25).collect(Collectors.toList()).get(0);
				String value25_1 = salaryRules25.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value25_2 = salaryRules25.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				userSalaryMonthDetail.setExpertPunish(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getJsPlanCounts() != null && userWithoutVo.getJsPlanCounts() > 0) {
							if (userWithoutVo.getJsPlanCounts() > userCounts) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getJsPlanCounts()).subtract(Convert.toBigDecimal(userCounts).multiply(Convert.toBigDecimal(value25_2)))).multiply(Convert.toBigDecimal(value25_1)).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setExpertPunish(bigDecimal);
								}
							}
						}
					}
				}

				//26技术支持奖励
				UserSalaryRules salaryRules26 = rulesList.stream().filter(o -> o.getRulesType() == 26).collect(Collectors.toList()).get(0);
				String value26_1 = salaryRules26.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value26_2 = salaryRules26.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				userSalaryMonthDetail.setExpertBonus(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getJsAwardPlanCounts() != null && userWithoutVo.getJsAwardPlanCounts() > 0) {
							BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getJsAwardPlanCounts()).subtract(Convert.toBigDecimal(value26_2)).multiply(Convert.toBigDecimal(value26_1)));
							if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
								userSalaryMonthDetail.setExpertBonus(bigDecimal);
							}
						}
					}
				}

				//27:保底工资
				UserSalaryRules salaryRules27 = rulesList.stream().filter(o -> o.getRulesType() == 27).collect(Collectors.toList()).get(0);
				List<SalaryVo> rulesDetail27 = salaryRules27.getRulesDetail();
				userSalaryMonthDetail.setMinSalary(Convert.toBigDecimal(rulesDetail27.get(0).getValue()));
				for (SalaryVo salaryVo : rulesDetail27) {
					if (userSalary.getSkillName().equals(salaryVo.getKey())) {
						userSalaryMonthDetail.setMinSalary(Convert.toBigDecimal(salaryVo.getValue()));
						break;
					}
				}


				//28:绩效工资
				BigDecimal performanceSalary = Convert.toBigDecimal(userSalaryMonthDetail.getUserSalary(), Convert.toBigDecimal(0)).add(Convert.toBigDecimal(userSalaryMonthDetail.getRoleSalary(), Convert.toBigDecimal(0))).add(Convert.toBigDecimal(userSalaryMonthDetail.getByPerformance(), Convert.toBigDecimal(0))).add(Convert.toBigDecimal(userSalaryMonthDetail.getWxPerformance(), Convert.toBigDecimal(0))).add(Convert.toBigDecimal(userSalaryMonthDetail.getNsPerformance(), Convert.toBigDecimal(0)));
				userSalaryMonthDetail.setPerformanceSalary(performanceSalary);

				//29:拟发工资
				if (performanceSalary.compareTo(userSalaryMonthDetail.getMinSalary()) == -1) {
					BigDecimal disAllSalary = userSalaryMonthDetail.getMinSalary().add(userSalaryMonthDetail.getExpertBonus()).subtract(userSalaryMonthDetail.getFailureRatePunish()).subtract(userSalaryMonthDetail.getByNoPassPunish()).subtract(userSalaryMonthDetail.getWxNoPassPunish()).subtract(userSalaryMonthDetail.getExpertPunish());
					userSalaryMonthDetail.setDisAllSalary(disAllSalary.setScale(2, BigDecimal.ROUND_HALF_UP));
				} else {
					BigDecimal disAllSalary = userSalaryMonthDetail.getPerformanceSalary().add(userSalaryMonthDetail.getExpertBonus()).subtract(userSalaryMonthDetail.getFailureRatePunish()).subtract(userSalaryMonthDetail.getByNoPassPunish()).subtract(userSalaryMonthDetail.getWxNoPassPunish()).subtract(userSalaryMonthDetail.getExpertPunish());
					userSalaryMonthDetail.setDisAllSalary(disAllSalary.setScale(2, BigDecimal.ROUND_HALF_UP));
				}

				if (userSalaryMonthDetail.getDisAllSalary().compareTo(Convert.toBigDecimal(0)) == 1) {
					userSalaryMonthDetail.setDisAllSalary(userSalaryMonthDetail.getDisAllSalary());
				} else {
					userSalaryMonthDetail.setDisAllSalary(Convert.toBigDecimal(0));
				}


				//30:主管奖励
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						userSalaryMonthDetail.setManagerBonus(userWithoutVo.getAwardPrices());
					}
				}

				//31:主管处罚
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						userSalaryMonthDetail.setManagerPunish(userWithoutVo.getPunishPrices());
					}
				}
				//维保绩效总工资
				BigDecimal enAllSalary = Convert.toBigDecimal(userSalaryMonthDetail.getDisAllSalary(), Convert.toBigDecimal(0)).add(Convert.toBigDecimal(userSalaryMonthDetail.getManagerBonus(), Convert.toBigDecimal(0))).subtract(Convert.toBigDecimal(userSalaryMonthDetail.getManagerPunish(), Convert.toBigDecimal(0))).setScale(2, BigDecimal.ROUND_HALF_UP);
				if (enAllSalary.compareTo(Convert.toBigDecimal(0)) == 1) {
					userSalaryMonthDetail.setEnAllSalary(enAllSalary);
				} else {
					userSalaryMonthDetail.setEnAllSalary(Convert.toBigDecimal(0));
				}

			} else {
				//维保员计算
				//6-1封装单人负责梯台---普通电梯1台计数1---年审梯两台计数1----1:年审0:非年审
				Double eleCounts1 = 0.00;
				if (listMap0.size() > 0 && listMap0.get(userSalary.getGroupId()) != null && listMap0.get(userSalary.getGroupId()).size() > 0) {
					eleCounts1 = Convert.toDouble(listMap0.get(userSalary.getGroupId()).size());
				}

				Double eleCounts2 = 0.00;
				if (listMap1.size() > 0 && listMap1.get(userSalary.getGroupId()) != null && listMap1.get(userSalary.getGroupId()).size() > 0) {
					eleCounts2 = Convert.toDouble(Convert.toBigDecimal(listMap1.get(userSalary.getGroupId()).size()).divide(Convert.toBigDecimal(2)));
				}
				int userCounts = listMap2.get(userSalary.getGroupId()).size();
				BigDecimal myElevatorCounts = Convert.toBigDecimal(eleCounts1 + eleCounts2).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP);
				userSalaryMonthDetail.setMyElevatorCounts(myElevatorCounts);

				//6-2封装年审和非年审数
				BigDecimal myElevatorNsCounts = Convert.toBigDecimal(eleCounts1).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP);
				userSalaryMonthDetail.setMyElevatorNsCounts(myElevatorNsCounts);

				//6-3封装非年审数量
				BigDecimal myElevatorVillaCounts = Convert.toBigDecimal(eleCounts2).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP);
				userSalaryMonthDetail.setMyElevatorVillaCounts(myElevatorVillaCounts);


				//7封装故障率----
				BigDecimal faultRate = Convert.toBigDecimal(0);
				if (userPlanMap2.size() > 0 && userPlanMap2.get(userSalary.getGroupId()) != null && userPlanMap2.get(userSalary.getGroupId()).size() > 0) {
					if ((eleCounts1 + eleCounts2) > 0) {
						faultRate = Convert.toBigDecimal(userPlanMap2.get(userSalary.getGroupId()).size()).divide(Convert.toBigDecimal(eleCounts1 + eleCounts2), 4, BigDecimal.ROUND_HALF_UP);
					}
				}
				userSalaryMonthDetail.setFaultRate(faultRate);


				//8封装-修工单担当比例
				BigDecimal myWxPlanRate = Convert.toBigDecimal(0);
				if (userPlanMap.get(userSalary.getUserId()) != null && userPlanMap.get(userSalary.getUserId()).size() > 0) {
					List<WxUserGroupPlanVo> userGroupPlanVos = userPlanMap3.get(userSalary.getGroupId());
					if (userGroupPlanVos != null && userGroupPlanVos.size() > 0) {
						myWxPlanRate = Convert.toBigDecimal(userPlanMap.get(userSalary.getUserId()).size()).divide(Convert.toBigDecimal(userGroupPlanVos.size()), 4, BigDecimal.ROUND_HALF_UP);
					}
				}

				userSalaryMonthDetail.setMyWxPlanRate(myWxPlanRate);

				//9-保养不合格单数
				userSalaryMonthDetail.setByNullifyPlanCounts(byPlanListMap.get(userSalary.getGroupId()) == null ? 0 : byPlanListMap.get(userSalary.getGroupId()).size());

				//10-维修不合格单数
				userSalaryMonthDetail.setWxNullifyPlanCounts(wxPlanListMap.get(userSalary.getUserId()) == null ? 0 : wxPlanListMap.get(userSalary.getUserId()).size());

				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);

						//11:申请技术支持次数
						userSalaryMonthDetail.setExpertHelpCounts(Convert.toInt(userWithoutVo.getJsPlanCounts(), 0));

						//12:技术支持判定-维修责任次数
						userSalaryMonthDetail.setWxExpertHelpCounts(Convert.toInt(userWithoutVo.getWxPlanCounts(), 0));

						//13:技术支持判定-保养责任次数
						userSalaryMonthDetail.setByExpertHelpCounts(Convert.toInt(userWithoutVo.getByPlanCounts(), 0));

						//14:技术支持他人次数
						userSalaryMonthDetail.setExpertHelpOtherCounts(Convert.toInt(userWithoutVo.getJsAwardPlanCounts(), 0));
					}
				}


				//.......................................................计算薪资.............................................................

				//15:基础工资--第一步已查出
				userSalaryMonthDetail.setUserSalary(userSalary.getSalary());
				//16:岗位薪资
				UserSalaryRules salaryRules = rulesList.stream().filter(o -> o.getRulesType() == 16).collect(Collectors.toList()).get(0);
				List<SalaryVo> rulesDetail = salaryRules.getRulesDetail();
				userSalaryMonthDetail.setRoleSalary(Convert.toBigDecimal(0));
				for (SalaryVo salaryVo : rulesDetail) {
					if (userSalary.getSkillName().equals(salaryVo.getKey())) {
						userSalaryMonthDetail.setRoleSalary(Convert.toBigDecimal(salaryVo.getValue()));
						break;
					}
				}

				//17:计算保养绩效
				getByPerformance(userSalaryMonthDetail, userSalary, rulesList);
				userSalaryMonthDetail.setByPerformance(userSalaryMonthDetail.getByPerformance().setScale(2, BigDecimal.ROUND_HALF_UP));

				//18:维修绩效--【(人员所在小组电梯总数*维修单价)*固定比例)/小组总人数】+【(人员所在小组电梯总数*维修单价)*平均比例*担当比例
				UserSalaryRules salaryRules3 = rulesList.stream().filter(o -> o.getRulesType() == 18).collect(Collectors.toList()).get(0);
				BigDecimal price_2 = Convert.toBigDecimal(salaryRules3.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue());
				SalaryVo wxSalaryVo = salaryRules3.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0);

				com.alibaba.fastjson.JSONObject jsonObjects = getWxPerformance(JSON.parseArray(wxSalaryVo.getValue()), faultRate);
				BigDecimal fixedRate = Convert.toBigDecimal(jsonObjects.get("fixedRate"));
				BigDecimal avgRate = Convert.toBigDecimal(jsonObjects.get("avgRate"));

				BigDecimal rate1 = Convert.toBigDecimal((eleCounts1 + eleCounts2)).multiply(price_2).multiply(fixedRate).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP);
				BigDecimal rate2 = Convert.toBigDecimal((eleCounts1 + eleCounts2)).multiply(price_2).multiply(avgRate).multiply(myWxPlanRate).setScale(2, BigDecimal.ROUND_HALF_UP);
				userSalaryMonthDetail.setWxPerformance(rate1.add(rate2));

				//19:年审绩效--年审单价
				UserSalaryRules salaryRules4 = rulesList.stream().filter(o -> o.getRulesType() == 19).collect(Collectors.toList()).get(0);
				BigDecimal price_3 = Convert.toBigDecimal(salaryRules4.getRulesDetail().get(0).getValue());
				BigDecimal nsPerformance = Convert.toBigDecimal(eleCounts1).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).divide(Convert.toBigDecimal(12), 2, BigDecimal.ROUND_HALF_UP).multiply(price_3).setScale(2, BigDecimal.ROUND_HALF_UP);
				userSalaryMonthDetail.setNsPerformance(nsPerformance);

				//20:故障率绩效处罚
				UserSalaryRules salaryRules5 = rulesList.stream().filter(o -> o.getRulesType() == 20).collect(Collectors.toList()).get(0);
				SalaryVo salaryVo1 = salaryRules5.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0);
				SalaryVo salaryVo2 = salaryRules5.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0);
				if ((faultRate.multiply(Convert.toBigDecimal(100))).compareTo(Convert.toBigDecimal(salaryVo1.getValue())) == 1) {
					BigDecimal bigDecimal = (userSalaryMonthDetail.getByPerformance().add(userSalaryMonthDetail.getWxPerformance())).multiply((userSalaryMonthDetail.getFaultRate().subtract(Convert.toBigDecimal(salaryVo1.getValue()).divide(Convert.toBigDecimal(100)))).multiply((Convert.toBigDecimal(salaryVo2.getValue()))));
					userSalaryMonthDetail.setFailureRatePunish(bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));
				} else {
					userSalaryMonthDetail.setFailureRatePunish(Convert.toBigDecimal(0));
				}

				//21:保养不合格处罚
				UserSalaryRules salaryRules21 = rulesList.stream().filter(o -> o.getRulesType() == 21).collect(Collectors.toList()).get(0);
				BigDecimal prices_21 = Convert.toBigDecimal(salaryRules21.getRulesDetail().get(0).getValue());
				if (userSalaryMonthDetail.getByNullifyPlanCounts() > 0) {
					BigDecimal decimal = Convert.toBigDecimal(userSalaryMonthDetail.getByNullifyPlanCounts()).multiply(prices_21).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP);
					if (decimal.compareTo(Convert.toBigDecimal(0)) == 1) {
						userSalaryMonthDetail.setByNoPassPunish(decimal);
					}

				}

				//22维修不合格处罚
				UserSalaryRules salaryRules22 = rulesList.stream().filter(o -> o.getRulesType() == 22).collect(Collectors.toList()).get(0);
				BigDecimal prices_22 = Convert.toBigDecimal(salaryRules22.getRulesDetail().get(0).getValue());
				if (userSalaryMonthDetail.getWxNullifyPlanCounts() > 0) {
					BigDecimal decimal = Convert.toBigDecimal(userSalaryMonthDetail.getByNullifyPlanCounts()).multiply(prices_22).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP);
					if (decimal.compareTo(Convert.toBigDecimal(0)) == 1) {
						userSalaryMonthDetail.setWxNoPassPunish(decimal);
					}
				}


				//23判断保养责任处罚---保养责任工单数--手动输入
				UserSalaryRules salaryRules23 = rulesList.stream().filter(o -> o.getRulesType() == 23).collect(Collectors.toList()).get(0);
				String value23_1 = salaryRules23.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value23_2 = salaryRules23.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();

				userSalaryMonthDetail.setByLiabilityPunish(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getByPlanCounts() != null && userWithoutVo.getByPlanCounts() > 0) {
							if (userWithoutVo.getByPlanCounts() > userCounts) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getByPlanCounts()).subtract(Convert.toBigDecimal(userCounts).multiply(Convert.toBigDecimal(value23_2)))).multiply(Convert.toBigDecimal(value23_1)).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setByLiabilityPunish(bigDecimal);
								}

							}
						}
					}
				}

				//24判断维修责任处罚---维修责任工单数--手动输入
				UserSalaryRules salaryRules24 = rulesList.stream().filter(o -> o.getRulesType() == 24).collect(Collectors.toList()).get(0);
				String value24_1 = salaryRules24.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value24_2 = salaryRules24.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				userSalaryMonthDetail.setWxLiabilityPunish(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getWxPlanCounts() != null && userWithoutVo.getWxPlanCounts() > 0) {
							if (userWithoutVo.getWxPlanCounts() > userCounts) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getWxPlanCounts()).subtract(Convert.toBigDecimal(userCounts).multiply(Convert.toBigDecimal(value24_2)))).multiply(Convert.toBigDecimal(value24_1)).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setWxLiabilityPunish(bigDecimal);
								}
							}
						}
					}
				}

				//25申请技术处罚--手动输入
				UserSalaryRules salaryRules25 = rulesList.stream().filter(o -> o.getRulesType() == 25).collect(Collectors.toList()).get(0);
				String value25_1 = salaryRules25.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value25_2 = salaryRules25.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				userSalaryMonthDetail.setExpertPunish(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getJsPlanCounts() != null && userWithoutVo.getJsPlanCounts() > 0) {
							if (userWithoutVo.getJsPlanCounts() > userCounts) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getJsPlanCounts()).subtract(Convert.toBigDecimal(userCounts).multiply(Convert.toBigDecimal(value25_2)))).multiply(Convert.toBigDecimal(value25_1)).divide(Convert.toBigDecimal(userCounts), 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setExpertPunish(bigDecimal);
								}
							}
						}
					}
				}

				//26技术支持奖励
				UserSalaryRules salaryRules26 = rulesList.stream().filter(o -> o.getRulesType() == 26).collect(Collectors.toList()).get(0);
				String value26_1 = salaryRules26.getRulesDetail().stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue();
				String value26_2 = salaryRules26.getRulesDetail().stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue();
				userSalaryMonthDetail.setExpertBonus(Convert.toBigDecimal(0));
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						if (userWithoutVo.getJsAwardPlanCounts() != null && userWithoutVo.getJsAwardPlanCounts() > 0) {
							if (userWithoutVo.getJsAwardPlanCounts() > Convert.toInt(value26_2)) {
								BigDecimal bigDecimal = (Convert.toBigDecimal(userWithoutVo.getJsAwardPlanCounts()).subtract(Convert.toBigDecimal(value26_2)).multiply(Convert.toBigDecimal(value26_1)));
								if (bigDecimal.compareTo(Convert.toBigDecimal(0)) == 1) {
									userSalaryMonthDetail.setExpertBonus(bigDecimal);
								}
							}

						}
					}
				}


				//27:保底工资
				UserSalaryRules salaryRules27 = rulesList.stream().filter(o -> o.getRulesType() == 27).collect(Collectors.toList()).get(0);
				List<SalaryVo> rulesDetail27 = salaryRules27.getRulesDetail();
				userSalaryMonthDetail.setMinSalary(Convert.toBigDecimal(rulesDetail27.get(0).getValue()));
				for (SalaryVo salaryVo : rulesDetail27) {
					if (userSalary.getSkillName().equals(salaryVo.getKey())) {
						userSalaryMonthDetail.setMinSalary(Convert.toBigDecimal(salaryVo.getValue()));
						break;
					}
				}

				//28:绩效工资
				BigDecimal performanceSalary = Convert.toBigDecimal(userSalaryMonthDetail.getUserSalary(), Convert.toBigDecimal(0)).add(Convert.toBigDecimal(userSalaryMonthDetail.getRoleSalary(), Convert.toBigDecimal(0))).add(Convert.toBigDecimal(userSalaryMonthDetail.getByPerformance(), Convert.toBigDecimal(0))).add(Convert.toBigDecimal(userSalaryMonthDetail.getWxPerformance(), Convert.toBigDecimal(0))).add(Convert.toBigDecimal(userSalaryMonthDetail.getNsPerformance(), Convert.toBigDecimal(0)));
				if (performanceSalary.compareTo(Convert.toBigDecimal(0)) == 1) {
					userSalaryMonthDetail.setPerformanceSalary(performanceSalary);
				}

				//29:拟发工资
				if (performanceSalary.compareTo(userSalaryMonthDetail.getMinSalary()) == -1) {
					BigDecimal disAllSalary = userSalaryMonthDetail.getMinSalary().add(userSalaryMonthDetail.getExpertBonus()).subtract(userSalaryMonthDetail.getFailureRatePunish()).subtract(userSalaryMonthDetail.getByNoPassPunish()).subtract(userSalaryMonthDetail.getWxNoPassPunish()).subtract(userSalaryMonthDetail.getExpertPunish());
					userSalaryMonthDetail.setDisAllSalary(disAllSalary.setScale(2, BigDecimal.ROUND_HALF_UP));
				} else {
					BigDecimal disAllSalary = userSalaryMonthDetail.getPerformanceSalary().add(userSalaryMonthDetail.getExpertBonus()).subtract(userSalaryMonthDetail.getFailureRatePunish()).subtract(userSalaryMonthDetail.getByNoPassPunish()).subtract(userSalaryMonthDetail.getWxNoPassPunish()).subtract(userSalaryMonthDetail.getByLiabilityPunish()).subtract(userSalaryMonthDetail.getWxLiabilityPunish()).subtract(userSalaryMonthDetail.getExpertPunish());
					userSalaryMonthDetail.setDisAllSalary(disAllSalary.setScale(2, BigDecimal.ROUND_HALF_UP));
				}

				if (userSalaryMonthDetail.getDisAllSalary().compareTo(Convert.toBigDecimal(0)) == 1) {
					userSalaryMonthDetail.setDisAllSalary(userSalaryMonthDetail.getDisAllSalary());
				}

				//30:主管奖励
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						userSalaryMonthDetail.setManagerBonus(userWithoutVo.getAwardPrices());
					}
				}

				//31:主管处罚
				if (userWithoutVos != null && userWithoutVos.size() > 0) {
					List<UserWithoutVo> withoutVos = userWithoutVos.stream().filter(o -> o.getUserId().equals(userSalary.getUserId())).collect(Collectors.toList());
					if (withoutVos != null && withoutVos.size() > 0) {
						UserWithoutVo userWithoutVo = withoutVos.get(0);
						userSalaryMonthDetail.setManagerPunish(userWithoutVo.getPunishPrices());
					}
				}
				//维保绩效总工资
				BigDecimal enAllSalary = Convert.toBigDecimal(userSalaryMonthDetail.getDisAllSalary(), Convert.toBigDecimal(0)).add(Convert.toBigDecimal(userSalaryMonthDetail.getManagerBonus(), Convert.toBigDecimal(0))).subtract(Convert.toBigDecimal(userSalaryMonthDetail.getManagerPunish(), Convert.toBigDecimal(0))).setScale(2, BigDecimal.ROUND_HALF_UP);
				if (enAllSalary.compareTo(Convert.toBigDecimal(0)) == 1) {
					userSalaryMonthDetail.setEnAllSalary(enAllSalary);
				}

			}
			userSalaryMonthDetail.setStatus(userSalary.getStatus());
			userSalaryMonthDetails.add(userSalaryMonthDetail);

		}
		if (userSalaryRules.getIsSave() == 1) {
			BigDecimal bigDecimal = Convert.toBigDecimal(0);
			for (UserSalaryMonthDetail userSalaryMonthDetail : userSalaryMonthDetails) {
				bigDecimal = bigDecimal.add(userSalaryMonthDetail.getEnAllSalary());
				userSalaryMonthDetail.setSalaryMonth(Convert.toInt(DateUtil.format(userSalaryRules.getBeginTime(), "yyyyMM")));
			}
			UserSalaryMonth userSalaryMonth = new UserSalaryMonth();
			userSalaryMonth.setSalaryMonth(Convert.toInt(DateUtil.format(userSalaryRules.getBeginTime(), "yyyyMM")));
			userSalaryMonth.setCreateUserName(AuthUtil.getNickName());
			userSalaryMonth.setSalaryMonthTime(userSalaryRules.getBeginTime());
			userSalaryMonth.setTemplateId(userSalaryRules.getTemplateId());
			userSalaryMonth.setSalaryAllPrices(bigDecimal);
			userSalaryMonth.setTemplateName(userSalaryRules.getTemplateName());
			userSalaryMonth.setUserCounts(userSalaryMonthDetails.size());
			userSalaryMonthService.save(userSalaryMonth);

			//保存明细
			List<List<UserSalaryMonthDetail>> partition = com.google.common.collect.Lists.partition(userSalaryMonthDetails, 20);
			for (List<UserSalaryMonthDetail> saveList : partition) {
				saveList.stream().forEach(o -> o.setSalaryMonthId(userSalaryMonth.getId()));
				userSalaryMonthDetailService.saveBatch(saveList);
			}
			List<UserSalaryRulesHistory> userSalaryRulesHistories = new ArrayList<>(34);
			for (UserSalaryRules userSalaryRule : rulesList) {
				UserSalaryRulesHistory userSalaryRulesHistory = new UserSalaryRulesHistory();
				BeanUtil.copyProperties(userSalaryRule, userSalaryRulesHistory);
				userSalaryRulesHistory.setId(null);
				userSalaryRulesHistory.setSalaryMonthId(userSalaryMonth.getId());
				userSalaryRulesHistories.add(userSalaryRulesHistory);
			}
			userSalaryRulesHistoryService.saveBatch(userSalaryRulesHistories);

			List<UserSalaryMonthDetail> resultList = new ArrayList(1);
			UserSalaryMonthDetail userSalaryMonthDetail = userSalaryMonthDetails.get(0);
			userSalaryMonthDetail.setId(userSalaryMonth.getId());
			resultList.add(userSalaryMonthDetail);
			return resultList;
		}
		return userSalaryMonthDetails;
	}


	/**
	 * 18 获取维修绩效
	 *
	 * @param jsonObjects
	 * @param faultRate
	 * @return
	 */
	private com.alibaba.fastjson.JSONObject getWxPerformance(JSONArray jsonObjects, BigDecimal faultRate) {
		Integer type = 9;
		if (faultRate.compareTo(Convert.toBigDecimal(0.05)) != 1) {
			type = 9;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.05)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.1)) != 1) {
			type = 8;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.1)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.15)) != 1) {
			type = 7;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.15)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.20)) != 1) {
			type = 6;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.20)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.30)) != 1) {
			type = 5;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.30)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.40)) != 1) {
			type = 4;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.40)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.50)) != 1) {
			type = 3;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.50)) == 1 && faultRate.compareTo(Convert.toBigDecimal(0.60)) != 1) {
			type = 2;
		} else if (faultRate.compareTo(Convert.toBigDecimal(0.60)) == 1) {
			type = 1;
		}
		for (int i = 0; i < jsonObjects.size(); i++) {
			com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonObjects.get(i).toString());
			if (Convert.toInt(jsonObject.get("faultRate")).equals(type)) {
				return jsonObject;
			}
		}
		return new com.alibaba.fastjson.JSONObject();

	}

	/**
	 * 17:计算保养绩效
	 *
	 * @param userSalaryMonthDetail
	 * @param userSalary
	 * @param rulesList
	 */
	private void getByPerformance(UserSalaryMonthDetail userSalaryMonthDetail, UserSalary userSalary, List<UserSalaryRules> rulesList) {
		UserSalaryRules salaryRules2 = rulesList.stream().filter(o -> o.getRulesType() == 17).collect(Collectors.toList()).get(0);
		List<SalaryVo> rulesDetail2 = salaryRules2.getRulesDetail();
		if (userSalary.getSkillName().equals("T0")) {
			userSalaryMonthDetail.setByPerformance(Convert.toBigDecimal(rulesDetail2.stream().filter(o -> o.getKeyId() == 4).collect(Collectors.toList()).get(0).getValue()));
		} else if (userSalaryMonthDetail.getIsExpert()) {
			userSalaryMonthDetail.setByPerformance(Convert.toBigDecimal(rulesDetail2.stream().filter(o -> o.getKeyId() == 3).collect(Collectors.toList()).get(0).getValue()));
		} else {
			Integer userElevatorCounts = Convert.toInt(userSalaryMonthDetail.getMyElevatorCounts().setScale(0, BigDecimal.ROUND_HALF_UP));
			if (userElevatorCounts <= 25) {
				userSalaryMonthDetail.setByPerformance(Convert.toBigDecimal(rulesDetail2.stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue()));
			} else {
				//保底
				userSalaryMonthDetail.setByPerformance(Convert.toBigDecimal(rulesDetail2.stream().filter(o -> o.getKeyId() == 2).collect(Collectors.toList()).get(0).getValue()));

				//单价
				Integer userElevatorCounts2 = Convert.toInt(userSalaryMonthDetail.getMyElevatorCounts().setScale(0, BigDecimal.ROUND_DOWN));
				Integer userElevatorCounts3 = Convert.toInt(userSalaryMonthDetail.getMyElevatorCounts().setScale(0, BigDecimal.ROUND_UP));
				//计算单价
				BigDecimal prices = Convert.toBigDecimal(rulesDetail2.stream().filter(o -> o.getKeyId() == 1).collect(Collectors.toList()).get(0).getValue());
				BigDecimal decimal1 = getPrices(userElevatorCounts2, userSalaryMonthDetail.getByPerformance(), prices);
				BigDecimal decimal2 = getPrices(userElevatorCounts3, userSalaryMonthDetail.getByPerformance(), prices);
				userSalaryMonthDetail.setByPerformance(decimal1.add((decimal2.subtract(decimal1)).multiply((userSalaryMonthDetail.getMyElevatorCounts().subtract(userSalaryMonthDetail.getMyElevatorCounts().setScale(0, BigDecimal.ROUND_DOWN))))));
			}
		}
	}


	private BigDecimal getPrices(Integer userElevatorCounts, BigDecimal byPerformance, BigDecimal prices) {
		if (userElevatorCounts >= 26) {
			byPerformance = byPerformance.add(prices);
		}
		if (userElevatorCounts >= 27) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1)));
		}
		if (userElevatorCounts >= 28) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1)));
		}

		if (userElevatorCounts >= 29) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.05)));
		}

		if (userElevatorCounts >= 30) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.1)));

		}

		if (userElevatorCounts >= 31) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.15)));

		}
		if (userElevatorCounts >= 32) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.20)));

		}
		if (userElevatorCounts >= 33) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.25)));
		}

		if (userElevatorCounts >= 34) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.3)));
		}
		if (userElevatorCounts >= 35) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.4)));
		}
		if (userElevatorCounts >= 36) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.5)));
		}
		if (userElevatorCounts >= 37) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.6)));
		}
		if (userElevatorCounts >= 38) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.7)));
		}
		if (userElevatorCounts >= 39) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.75)));
		}
		if (userElevatorCounts >= 40) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.8)));
		}
		if (userElevatorCounts >= 41) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.85)));
		}
		if (userElevatorCounts >= 42) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.9)));
		}
		if (userElevatorCounts >= 43) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.95)));
		}
		if (userElevatorCounts >= 44) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(2)));
		}
		if (userElevatorCounts >= 45) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.9)));
		}
		if (userElevatorCounts >= 46) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.8)));
		}
		if (userElevatorCounts >= 47) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.7)));
		}
		if (userElevatorCounts >= 48) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.5)));
		}
		if (userElevatorCounts >= 49) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.3)));
		}
		if (userElevatorCounts >= 50) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(1.1)));
		}
		if (userElevatorCounts >= 51) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(0.9)));
		}
		if (userElevatorCounts >= 52) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(0.7)));
		}
		if (userElevatorCounts >= 53) {
			byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(0.5)));
		}
		//如果大于53台--单价*台数*0.5
		if (userElevatorCounts > 53) {
			for (int i = 0; i < (userElevatorCounts - 53); i++) {
				byPerformance = byPerformance.add(prices.multiply(Convert.toBigDecimal(0.5)));
			}
		}
		return byPerformance;
	}


	private Map<Long, List<WxUserGroupPlanVo>> getFaultRateMap(UserSalaryRules userSalaryRules, List<UserSalary> list) {
		List<WxUserGroupPlanVo> wxUserGroupPlanVos = baseMapper.getWxUserGroupPlanVo(userSalaryRules);
		List<WxUserGroupPlanVo> wxUserGroupPlanVos2 = new ArrayList<>(100);
		for (WxUserGroupPlanVo wxUserGroupPlanVo : wxUserGroupPlanVos) {
			for (UserSalary userSalary : list) {
				if (wxUserGroupPlanVo.getGroupId().equals(userSalary.getGroupId())) {
					wxUserGroupPlanVos2.add(wxUserGroupPlanVo);
				}
			}
		}
		return wxUserGroupPlanVos2.stream().distinct().collect(Collectors.groupingBy(WxUserGroupPlanVo::getGroupId));
	}


	private Map<Long, List<WxUserGroupPlanVo>> getMyWxPlanRateGroupId(List<UserSalary> list, List<WxUserGroupPlanVo> wxUserGroupPlanVos) {
		List<WxUserGroupPlanVo> wxUserGroupPlanVos3 = new ArrayList<>(100);

		for (WxUserGroupPlanVo wxUserGroupPlanVo : wxUserGroupPlanVos) {
			for (UserSalary userSalary : list) {
				if (wxUserGroupPlanVo.getUserId().equals(userSalary.getUserId()) && wxUserGroupPlanVo.getGroupId().equals(userSalary.getGroupId())) {
					wxUserGroupPlanVos3.add(wxUserGroupPlanVo);
				}
			}
		}
		return wxUserGroupPlanVos3.stream().distinct().collect(Collectors.groupingBy(WxUserGroupPlanVo::getGroupId));
	}


	private Map<Long, List<WxUserGroupPlanVo>> getMyWxPlanRateUserId(List<UserSalary> list, List<WxUserGroupPlanVo> wxUserGroupPlanVos) {

		List<WxUserGroupPlanVo> wxUserGroupPlanVos3 = new ArrayList<>(100);
		for (WxUserGroupPlanVo wxUserGroupPlanVo : wxUserGroupPlanVos) {
			for (UserSalary userSalary : list) {
				if (wxUserGroupPlanVo.getUserId().equals(userSalary.getUserId()) && wxUserGroupPlanVo.getGroupId().equals(userSalary.getGroupId())) {
					wxUserGroupPlanVos3.add(wxUserGroupPlanVo);
				}
			}
		}
		return wxUserGroupPlanVos3.stream().distinct().collect(Collectors.groupingBy(WxUserGroupPlanVo::getUserId));
	}


	/**
	 * 2023-11-03 需求变更。
	 * 复用强哥的维修方法
	 * 封装数据大屏维修数据
	 *
	 * @param tasks                任务list
	 * @param elevatorEffectedList 有效梯list
	 * @return 结果
	 */
	private BigDecimal handleMalfunctionScreenDataBetweenDay(List<MaintainPlanContentVO> tasks, List<ElevatorEffectedVO> elevatorEffectedList, Integer betweenDay) {
		MalfunctionScreenVO malfunctionScreenVO = new MalfunctionScreenVO();
		long applyRepairTotal = tasks.stream().filter(t -> TaskStatusEnum.WX_CLOSED.getStatus() != t.getStatus() || TaskStatusEnum.WX_TURN_DOWN.getStatus() != t.getStatus()).count();
		malfunctionScreenVO.setApplyRepairTotal(applyRepairTotal);

		// 新增维修任务 (维修任务状态为【待开始】/【进行中】/【已完成】的维修任务工单总数)
		List<Integer> repairTaskStatus = Arrays.asList(TaskStatusEnum.WX_TO_START.getStatus(), TaskStatusEnum.WX_UNDER_WAY.getStatus(), TaskStatusEnum.WX_SIGN_FINISH.getStatus());
		long repairTaskTotal = tasks.stream().filter(t -> repairTaskStatus.contains(t.getStatus())).count();
		malfunctionScreenVO.setRepairTaskTotal(repairTaskTotal);

		// 有效梯
		long elevatorTotal = Func.isEmpty(elevatorEffectedList) ? 0 : elevatorEffectedList.stream().map(t -> t.getElevatorId()).distinct().count();
		// 第一次维修任务数
		long firstPlanTotal = tasks.stream().filter(t -> repairTaskStatus.contains(t.getStatus())).map(t -> t.getElevatorId()).distinct().count();
		// 重复故障任务数
		long repeatPlanTotal = repairTaskTotal - firstPlanTotal;
		if (elevatorTotal > 0) {
			// 梯故障率 = 故障电梯总数(新增维修任务)/生效电梯数
			BigDecimal eleFaultRatio = new BigDecimal(repairTaskTotal).divide(new BigDecimal(elevatorTotal), 3, BigDecimal.ROUND_HALF_UP);
			malfunctionScreenVO.setEleFaultRatio(eleFaultRatio);
			return eleFaultRatio.multiply(BigDecimal.valueOf(30)).divide(BigDecimal.valueOf(betweenDay), 3, RoundingMode.HALF_UP);
		} else {
			return Convert.toBigDecimal(0);
		}
	}

}
